/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.general.location;

import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.TextBoxDoubleValueBlurHandler;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.common.ui.TextBoxLongValueBlurHandler;
import gov.fema.hazus.bdc.client.general.GeneralConstants;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.Location;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LocationForm extends AbstractForm implements BuildingUpdate {

	// labels
	private Label latitude;
	private Label longitude;
	private Label censusBlock;
	private Label censusGroup;
	private Label censusTract;
	private Label countyFIPS;
	
	// form fields
	private TextBox formLatitude = new TextBox();
	private TextBox formLongitude = new TextBox();
	private TextBox formCensusBlock = new TextBox();
	private TextBox formCensusGroup = new TextBox();
	private TextBox formCensusTract = new TextBox();
	private TextBox formCountyFIPS = new TextBox();
	
	// ui components
	private DockPanel docker;
	private FlexTable table = new FlexTable(); // Grid( 7,2)
    private HTML locator = new HTML();
    
    private LocationFinder finder = new LocationFinder();
	
	// entities
	private Location location;
	
	// listener
	private LocationTextBoxKeyListener locationTextBoxKeyListener;
	private GeneralConstants constants;

	private TextBoxDoubleValueBlurHandler textBoxDoubleValueBlurHandler;
	
	public LocationForm(Building building) {
		
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (GeneralConstants)GWT.create(GeneralConstants.class);
		tabTitle = constants.generalLocationTabTitle();
		
        // set up locator
        locator = new HTML( constants.generalLocationLocateBuilding() );
        locator.setStyleName("clickable-text");
        
        //TODO: hook up map via JSNI
        //finder.setLocationForm(this);
		locator.addClickListener(finder);
        
		// set up labels
		latitude = new Label(constants.generalLocationLatitude());
		longitude = new Label(constants.generalLocationLongitude());
		censusBlock = new Label(constants.generalLocationCensusBlock());
		censusGroup = new Label(constants.generalLocationCensusGroup());
		censusTract = new Label(constants.generalLocationCensusTract());
		countyFIPS = new Label(constants.generalLocationCountyFIPS());
		
		// add labels to form
		table.setWidget(0, 0, latitude);
		table.setWidget(1, 0, longitude);
		table.setWidget(2, 0, censusBlock);
		table.setWidget(3, 0, censusGroup);
		table.setWidget(4, 0, censusTract);
		table.setWidget(5, 0, countyFIPS);
		
		// get entities
		location = building.getLocation();
		
		// listener
		locationTextBoxKeyListener = new LocationTextBoxKeyListener(location);
		textBoxDoubleValueBlurHandler = new TextBoxDoubleValueBlurHandler();
		
		// attach listeners
		loadListeners();
		
		// load forms
		loadForms();
				
		// add fields to table
		table.setWidget(0, 1, formLatitude);
		table.setWidget(1, 1, formLongitude);
        table.setWidget(1, 2, locator);
		table.setWidget(2, 1, formCensusBlock);
		table.setWidget(3, 1, formCensusGroup);
		table.setWidget(4, 1, formCensusTract);
		table.setWidget(5, 1, formCountyFIPS);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);

	}

	public void updateBuilding(Building building) {
		
		// entities
		this.building = building;
		this.location = building.getLocation();
		
		// listeners
		locationTextBoxKeyListener.updateGeoLocation(location);
		
		// reload forms
		loadForms();
	}

	private void loadForms()
	{
		
		if( location != null )
		{
			// convert lat long to doubles
			String lat = Double.toString(location.getLatitude());
			formLatitude.setText( lat );
			formLatitude.setName( Location.FIELD_LATITUDE );
			
			String lng = Double.toString(location.getLongitude());
			formLongitude.setText( lng );
			formLongitude.setName( Location.FIELD_LONGITUDE );
			
			formCensusBlock.setText( location.getCensusBlock() );
			formCensusBlock.setName( Location.FIELD_CENSUSBLOCK );
			
			formCensusGroup.setText( location.getCensusGroup() );
			formCensusGroup.setName( Location.FIELD_CENSUSGROUP );
			
			formCensusTract.setText( location.getCensusTract() );
			formCensusTract.setName( Location.FIELD_CENSUSTRACT );
			
			formCountyFIPS.setText( location.getCountyFIPS() );
			formCountyFIPS.setName( Location.FIELD_COUNTYFIPS );
        
		}
		if( building != null )
		{
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formLatitude.setEnabled(false);
	            formLongitude.setEnabled(false);
	            formCensusBlock.setEnabled(false);
	            formCensusGroup.setEnabled(false);
	            formCensusTract.setEnabled(false);
	            formCountyFIPS.setEnabled(false);
	        } else
	        {
	            formLatitude.setEnabled(true);
	            formLongitude.setEnabled(true);
	            formCensusBlock.setEnabled(true);
	            formCensusGroup.setEnabled(true);
	            formCensusTract.setEnabled(true);
	            formCountyFIPS.setEnabled(true);
	        }
		}
	
	}
	
	private void loadListeners()
	{
		formLatitude.addFocusListener(locationTextBoxKeyListener);
		formLatitude.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formLongitude.addFocusListener(locationTextBoxKeyListener);
		formLongitude.addBlurHandler(textBoxDoubleValueBlurHandler);
		
		formCensusBlock.addFocusListener( locationTextBoxKeyListener );
		formCensusGroup.addFocusListener( locationTextBoxKeyListener );
		formCensusTract.addFocusListener( locationTextBoxKeyListener );
		formCountyFIPS.addFocusListener( locationTextBoxKeyListener );
	}

	/* TODO: hook up map via JSNI
    public void setPointAsLocation(LatLng point) {
        
        String lat = Double.toString(point.getLatitude());
        formLatitude.setText( lat );
        location.setLatitude(point.getLatitude());
        
        String lng = Double.toString(point.getLongitude());
        formLongitude.setText( lng );
        location.setLongitude(point.getLongitude());
        
    }
    */
    
    public Location getLocation()
    {
        return location;
    }

    public void updateLatLng(final double latitude, final double longitude)
    {
    	GWT.log("Setting lat/lng text in form: lat("+latitude+"), lng("+longitude+")", null);
    	
    	if( !building.getMode().equalsIgnoreCase(Building.READ_ONLY))
    	{
    		// update the form
        	String lat = Double.toString(latitude);
        	String lng = Double.toString(longitude);
        	
        	this.formLatitude.setText(lat);
        	this.formLongitude.setText(lng);
        	
        	// update the Location
        	location.setLatitude(latitude);
        	location.setLongitude(longitude);	
    	}    	
    }
}
